package main

import (
	"fmt"
)

func main() {

	var ss = " 1开 始 2 "
	star := 0
	end := 9
	a := SubString(ss, star, end)
	fmt.Println(ss,a)
	b := trimTest(ss)
	fmt.Println(b)

}

func trimTest(str string) (newStr string) {

	// 将字符串的转换成[]rune
	strList := []rune(str)
	len := len(strList)
	star := 0
	end  := len - 1

	for i:=0; i<len; i++ {
		if star == i {
			if string(strList[i:i+1]) == " " {
				star ++
			}
		}else {
			if string(strList[i:i+1]) == " " {
				end = i
			}
		}
	}

	return string(strList[star:end])
}

func SubString(str string,begin,length int) (substr string) {

	// 将字符串的转换成[]rune
	rs := []rune(str)
	lth := len(rs)

	// 简单的越界判断
	if begin < 0 {
		begin = 0
	}
	if begin >= lth {
		begin = lth
	}
	end := begin + length
	if end > lth {
		end = lth
	}

	if begin == 0 && end < lth {
		substr = string(rs[begin:end]) + "..."
	}else {
		substr = string(rs[begin:end])
	}
	return
}